package com.mybatisPlus;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.Properties;

//代码自动生成器
public class Code {
    public static void main(String[] args) {
        //构建一个代码生成器对象
        AutoGenerator gc = new AutoGenerator();

        //配置执行策略
        //1.全局配置
        GlobalConfig config = new GlobalConfig();
        //输出路径
        String propertyPath = System.getProperty("user.dir");
        System.out.println(propertyPath);
        config.setOutputDir(propertyPath+"/src/main/java");
        //输出作者
        config.setAuthor("lusheng");
        //不打开window文件夹
        config.setOpen(true);
        //文件是覆盖
        config.setFileOverride(true);
        //去Service的I前缀
        config.setServiceName("%sService");
        //设置ID生成策略
        config.setIdType(IdType.ID_WORKER);
        //日期时间格式
        config.setDateType(DateType.ONLY_DATE);
        //生成swagger文档
        config.setSwagger2(true);
        //填充生成器数据
        gc.setGlobalConfig(config);

        //2.设置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=utf-8");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        dsc.setDbType(DbType.MYSQL);
        gc.setDataSource(dsc);

        //3.包的配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("mybatisPlus");
        pc.setParent("com.mybatisPlus");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        gc.setPackageInfo(pc);

        //4.策略配置
        StrategyConfig strategy = new StrategyConfig();
        //设置要映射的表名
        strategy.setInclude("user");
        //列名下划线转驼峰命名
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);

        //自动生成Lombok
        strategy.setEntityLombokModel(true);
        //设置逻辑删除字段
        strategy.setLogicDeleteFieldName("deleted");
        //自动填充配置
        TableFill gmtCreate = new TableFill("create_time", FieldFill.INSERT);
        TableFill gmtModified = new TableFill("update_time",FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(gmtCreate);
        tableFills.add(gmtModified);
        strategy.setTableFillList(tableFills);

        //乐观锁
        strategy.setVersionFieldName("version");
        //restful风格
        strategy.setRestControllerStyle(true);
        //驼峰转连字符
        strategy.setControllerMappingHyphenStyle(true);
        gc.setStrategy(strategy);

        gc.execute();
    }
}
